"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
#coding:utf-8
import string
from django.test import TestCase
import time
from BigPrize import models
from BigPrize.GameRule.BP import BigPrize

__author__ = 'zhouqi'

class BPTest(TestCase):
    def test_ComputeDrawPrz(self):
        vote = models.UserVote()
        vote.voteId = 1
        vote.USER_ID = 1
        vote.DRAW_ID = 1
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(1, 2):
            win = models.UserWin()
            win.VOTE_ID = 1
            win.USER_ID = 1
            win.DRAW_ID = 1
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.VOTE_ID) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0

        self.assertEqual(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.USER_ID = 1
        vote.DRAW_ID = 1
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(1, 5, 3):
            win = models.UserWin()
            win.VOTE_ID = 1
            win.USER_ID = 1
            win.DRAW_ID = 1
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.VOTE_ID) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 5
        wins[1].prize = 0

        self.assertEqual(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.USER_ID = 1
        vote.DRAW_ID = 1
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2, 3]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in [1, 2, 4, 5]:
            win = models.UserWin()
            win.VOTE_ID = 1
            win.USER_ID = 1
            win.DRAW_ID = 1
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2, 3]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.VOTE_ID) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 3
        wins[1].prize = 0
        wins[2].winChance = 5
        wins[2].prize = 3000
        wins[3].winChance = 15
        wins[3].prize = 600

        self.assertEqual(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.USER_ID = 1
        vote.DRAW_ID = 1
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 1, 2, 3, 4]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(1, 9):
            win = models.UserWin()
            win.VOTE_ID = 1
            win.USER_ID = 1
            win.DRAW_ID = 1
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 1, 2, 3, 4]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.VOTE_ID) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 5
        wins[1].prize = 0

        self.assertEqual(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)



    def test_Encode(self):
        testDict = (('1,2,3,4,5;1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5;1,2,3', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3]), ''))
                    , ('1,2,3,4,5;1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5;1,2,3;6;', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127]), ''))
                    , ('1,2,3,4,5;1,2,3;6;4', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 4]), '')))
        for k, v in testDict:
            self.assertEquals(BigPrize.Encode(k), v)

    def test_Count(self):
        testDict = ((string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''), 1)
                    # multiple
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), ''), 6)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3]), ''), 3)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 127]), ''), 3)
                    # dan & tuo
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127]), ''), 15)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 4]), ''), 15))
        for k, v in testDict:
            self.assertEquals(BigPrize.Count(k), v)

        errorDict = (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6]), '')
                     , string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 127]), '')
                     , string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127]), ''))
        for k in errorDict:
            self.assertRaises(Exception, BigPrize.Count, k)

    def test_Find(self):
        self.assertEquals(BigPrize.Find([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])

    def test_GetRightNum(self):
        self.assertEquals(BigPrize.GetRightNum(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''),
                                               string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')), [5, 2, 0, 0])
